package com.borball.store.domain;

import java.io.Serializable;

public class Composite implements Serializable {

	private static final long serialVersionUID = 5471849623394365609L;

	private String name;
	private String description;
	private String units;
	private String unit;

	public String getName() {
		return name;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getUnits() {
		return units;
	}

	public void setUnits(String units) {
		this.units = units;
	}

	public UnitAmount[] string2UnitAmount() {
		String[] composites = units.split("\\|");
		UnitAmount[] uas = new UnitAmount[composites.length];

		int i = 0;
		for (String c : composites) {
			String[] kv = c.split("\\:");
			UnitAmount uai = new UnitAmount();
			uai.setName(kv[0]);

			if(kv.length==1){
				uai.setAmount(0);
			} else {
				uai.setAmount(Integer.valueOf(kv[1]));
			}
			uas[i] = uai;
			i++;
		}

		return uas;
	}

	public String unitAmount2String(UnitAmount[] uas) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < uas.length; i++) {
			sb.append(uas[i].getName()).append(":").append(uas[i].getAmount()).append("|");
		}

		return sb.toString().substring(0, sb.length() - 1);
	}
}
